/*!
 * Ext JS Library 3.2.2
 * Copyright(c) 2006-2010 Ext JS, Inc.
 * licensing@extjs.com
 * http://www.extjs.com/license
 */

// Sample desktop configuration

function getCookie(c_name){
	if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return decodeURI(document.cookie.substring(c_start,c_end));
    }
  }
	return "";
}

MyDesktop = new Ext.app.App({
	init :function(){
		Ext.QuickTips.init();
		
		new Ajax.Request("/desktop/current_user", { 
			method: "POST",
			//parameters: pars,
			onComplete:	 function(request) {
				var user = eval("("+request.responseText+")").users;
				currentUser = user;
				
				var pars = {yg_id:currentUser.id}
				new Ajax.Request("/desktop/get_prev_code", { 
					method: "POST",
					parameters: pars,
					onComplete:	 function(request) {
						currentUser.prev_code = request.responseText;
					}
				});
				
			}
		});
		
		
	},

	getModules : function(){
		return [
			new MyDesktop.HouseWindow(),
			new MyDesktop.GridWindow(),
			new MyDesktop.TabWindow(),
			new MyDesktop.AccordionWindow(),
			new MyDesktop.PreferenceWindow()
			//new MyDesktop.BogusMenuModule(),
			//new MyDesktop.BogusModule()
		];
	},

	// config for the start menu
	getStartConfig : function(){
			return {
					title: getCookie('loginName'),
					iconCls: 'user',
					toolItems: [{
							text:'修改密码',
							iconCls:'settings',
							scope:this,
							handler:function(){
								 window.location = "/change_password";
							}							
					},'-',{
							text:'退出',
							iconCls:'logout',
							scope:this,
							handler:function(){
								 window.location = "/logout";
							}
							
					}]
			};
		
	}

});



/*
 * Example windows
 */
MyDesktop.HouseWindow = Ext.extend(Ext.app.Module, {
		id:'grid-house1',
		init : function(){
				this.launcher = {
						text: '客源列表',
						iconCls:'icon-house1',
						handler : this.createWindow,
						scope: this
				}
		},

		createWindow : function(){
				
				if (currentUser.prev_code[8] != 't') {
						//Ext.Msg.alert('Message','权限不够. 请与管理员联系后再试！');
						msg('Message','权限不够. 请与管理员联系后再试！');
						return;
				}
			
				var desktop = this.app.getDesktop();
				var win = desktop.getWindow('grid-house1');
				if(!win){
						qzxx_id = 0;
						win = desktop.createWindow({
								id: 'grid-house1',
								title:'客源列表',
								x:100,
								width:1040,
								height:600,
								iconCls: 'icon-grid',
								shim:false,
								animCollapse:false,
								constrainHeader:true,
								layout: 'fit',
								items: new QzxxPanel(this,{})
						});
				}
				win.show();
		}
});



MyDesktop.GridWindow = Ext.extend(Ext.app.Module, {
		id:'grid-win',
		init : function(){
				this.launcher = {
						text: '房源列表',
						iconCls:'icon-house2',
						handler : this.createWindow,
						scope: this
				}
		},

		createWindow : function(){
			
				if (currentUser.prev_code[13] != 't') {
						//Ext.Msg.alert('Message','权限不够. 请与管理员联系后再试！');
						msg('Message','权限不够. 请与管理员联系后再试！');
						return;
				}
			
				var desktop = this.app.getDesktop();
				var win = desktop.getWindow('grid-win');
				if(!win){
						czxx_id = 0;
						win = desktop.createWindow({
								id: 'grid-win',
								title:'房源列表',
								x:100,
								width:1040,
								height:600,
								iconCls: 'icon-grid',
								shim:false,
								animCollapse:false,
								constrainHeader:true,
								layout: 'fit',
								items: new CzxxPanel(this,{})
						});
				}
				win.show();
		}
});



MyDesktop.TabWindow = Ext.extend(Ext.app.Module, {
		id:'grid-award',
		init : function(){
				this.launcher = {
						text: '排行榜',
						iconCls:'icon-award',
						handler : this.createWindow,
						scope: this
				}
		},

		createWindow : function(){

			if (currentUser.prev_code[23] != 't') {
					//Ext.Msg.alert('Message','权限不够. 请与管理员联系后再试！');
					msg('Message','权限不够. 请与管理员联系后再试！');
					return;
			}			
			
				var desktop = this.app.getDesktop();
				var win = desktop.getWindow('tab-win');
				if(!win){
						win = desktop.createWindow({
								id: 'tab-win',
								title:'统计报表',
								x:100,
								width:1040,
								height:680,
								iconCls: 'icon-award',
								shim:false,
								animCollapse:false,
								border:false,
								constrainHeader:true,

								layout: 'fit',
								items: new HotPanel(this, {})
								
								/*
										new Ext.TabPanel({
												activeTab:0,

												items: [{
														title: 'Tab Text 1',
														header:false,
														html : '<p>Something useful would be in here.</p>',
														border:false
												},{
														title: 'Tab Text 2',
														header:false,
														html : '<p>Something useful would be in here.</p>',
														border:false
												},{
														title: 'Tab Text 3',
														header:false,
														html : '<p>Something useful would be in here.</p>',
														border:false
												},{
														title: 'Tab Text 4',
														header:false,
														html : '<p>Something useful would be in here.</p>',
														border:false
												}]
										})
								*/		
						});
				}
				win.show();
		}
});



MyDesktop.AccordionWindow = Ext.extend(Ext.app.Module, {
		id:'acc-win',
		init : function(){
				this.launcher = {
						text: '联系人',
						iconCls:'accordion',
						handler : this.createWindow,
						scope: this
				}
		},

		createWindow : function(){
			
			if (currentUser.prev_code[18] != 't') {
					//Ext.Msg.alert('Message','权限不够. 请与管理员联系后再试！');
					msg('Message','权限不够. 请与管理员联系后再试！');
					return;
			}
			
				var desktop = this.app.getDesktop();
				var win = desktop.getWindow('acc-win');
				if(!win){
						win = desktop.createWindow({
								id: 'acc-win',
								title: '联系人',
								width:250,
								height:400,
								x : 100,
								y : 150,
								iconCls: 'accordion',
								shim:false,
								animCollapse:false,
								constrainHeader:true,

								tbar:[{
										tooltip:'增加',
										iconCls:'user-add'
								},' ',{
										tooltip:'删除',
										iconCls:'user-delete'
								},'->',
								new Ext.form.TextField({
									id : 'fiter-user',
									width:100,
									enableKeyEvents: true
								}),' ', {
									text: '查找',
									iconCls :'search',
									handler : function(){
										var tree = Ext.getCmp('kh-tree');
										tree.body.mask('Loading', 'x-mask-loading');
										tree.loader.baseParams = { yg_id:currentUser.id, filter: Ext.getCmp('fiter-user').getValue()},
										tree.root.reload();
										tree.root.collapse(true, false);
										setTimeout(function(){ // mimic a server call
												tree.body.unmask();
												tree.root.expand(true, true);
										}, 1000);										
									}
								}],

								layout:'accordion',
								border:false,
								layoutConfig: {
										animate:false
								},

								items: [
										new Ext.tree.TreePanel({
												id:'yg-tree',
												title: '员工',
												rootVisible:false,
												lines:false,
												autoScroll:true,
												tools:[{
														id:'refresh',
														on:{
																click: function(){
																		var tree = Ext.getCmp('yg-tree');
																		tree.body.mask('Loading', 'x-mask-loading');
																		tree.root.reload();
																		tree.root.collapse(true, false);
																		setTimeout(function(){ // mimic a server call
																				tree.body.unmask();
																				tree.root.expand(true, true);
																		}, 1000);
																}
														}
												}],
												loader: new Ext.tree.TreeLoader({
													dataUrl: '/desktop/get_ygtree',
													baseAttrs: { uiProvider: Ext.ux.TreeCheckNodeUI } 
												}),
												root: {
													nodeType: 'async',
													text: '联系人',
													draggable:false,
													id:'root'
												}
										}), 
										new Ext.tree.TreePanel({
												id:'kh-tree',
												title: '客户',
												rootVisible:false,
												lines:false,
												autoScroll:true,
												tools:[
													{
														id:'refresh',
														on:{
																click: function(){
																		var tree = Ext.getCmp('kh-tree');
																		tree.body.mask('Loading', 'x-mask-loading');
																		tree.root.reload();
																		tree.root.collapse(true, false);
																		setTimeout(function(){ // mimic a server call
																				tree.body.unmask();
																				tree.root.expand(true, true);
																		}, 1000);
																}
														}
												}],
												loader: new Ext.tree.TreeLoader({
													dataUrl: '/desktop/get_khtree',
													baseAttrs: { uiProvider: Ext.ux.TreeCheckNodeUI },
													baseParams: { yg_id:currentUser.id } 
												}),
												root: {
													nodeType: 'async',
													text: '联系人',
													draggable:false,
													id:'root'
												}
										}),
										{
												title: '个人提醒',
												//html : '<p>Something useful would be in here.</p>',
												autoScroll: true,
												autoLoad: {url: '/desktop/get_reminder', params: 'id='+currentUser.id}
										},{
												title: '我的收藏',
												html : '<p>Something useful would be in here.</p>'
										}
								]
						});
						
						win.on('activate', function(p){
							//p.items.items[2].autoLoad.params = 'id='+currentUser.id;
							//msg('Click','You clicked on "查地图".');
						});
				};

				
				
				var send_message =function() {
					var sms_win = new Ext.Window({
							title: '发送短信',
							floating: true,
							shadow: true,
							draggable: true,
							closable: true,
							resizable: false,
							modal: true,
							width: 330,
							height: 300,
							layout: 'fit',
							plain: true,
							items: [{
								xtype:"form",
								layout: 'absolute',
								id : 'sms_msg_form',
						    items: [{
						            xtype: 'label',
						            text: '接收人',
						            x: 20,
						            y: 10
						        },
						        {
						            xtype: 'label',
						            text: '短信内容',
						            x: 10,
						            y: 40
						        },
										{
						            xtype: 'textfield',
						            x: 70,
						            y: 10,
						            width: 230,
						            name: 'receiver'
												
						        },
						        {
						            xtype: 'textarea',
						            x: 70,
						            y: 40,
						            width: 230,
						            height: 170,
						            name: 'sms_text',
												value: "\n/"+getCookie('loginName')
						    }],
								buttons:[{
									text: '发送',
									handler: function(){
										var pars = Ext.getCmp("sms_msg_form").getForm().getValues();
										//pars['sms_text'] = encodeURIComponent(pars['sms_text']);
										new Ajax.Request("/desktop/send_sms", { 
											method: "POST",
											parameters: pars,
											onComplete:	 function(request) {
												sms_win.close();
											}
										});
									}
								},{
									text: '取消',
									handler: function(){
										sms_win.close();
									}
								}]
							}]
					});

					var node = Ext.getCmp('acc-win').layout.activeItem.selModel.selNode;
					var ss = node.id.split("|")
					
					Ext.getCmp('sms_msg_form').items.items[2].setValue(ss[2]);
					
					sms_win.on("activate",function(e){
						this.toFront()
					});

					sms_win.show();

				};
				
				var view_detail = function() {
					var detail_win = new Ext.Window({
						title: '详细情况',
						floating: true,
						shadow: true,
						draggable: true,
						closable: true,
						resizable: false,
						modal: true,
						width: 330,
						height: 300,
						layout: 'fit',
						plain: true,
						items: [{
							xtype:"form",
							layout: 'absolute',
							id : 'view_detail_form',
					    items: [{
					            xtype: 'label',
					            text: '姓名',
					            x: 20,
					            y: 10
					        },
					        {
					            xtype: 'label',
					            text: '部门',
					            x: 20,
					            y: 40
					        },
					        {
					            xtype: 'label',
					            text: '电话',
					            x: 20,
					            y: 70
					        },
					        {
					            xtype: 'label',
					            text: '家庭地址',
					            x: 20,
					            y: 100
					        },
					        {
					            xtype: 'label',
					            text: '邮件',
					            x: 20,
					            y: 130
					        },
									{
					            xtype: 'textfield',
					            x: 70,
					            y: 10,
					            width: 230,
					        },
									{
					            xtype: 'textfield',
					            x: 70,
					            y: 40,
					            width: 230,
					        },
									{
					            xtype: 'textfield',
					            x: 70,
					            y: 70,
					            width: 230,
						      },
									{
					            xtype: 'textfield',
					            x: 70,
					            y: 100,
					            width: 230,
					        },
									{
					            xtype: 'textfield',
					            x: 70,
					            y: 130,
					            width: 230,
					    }]
						}],					
						buttons:[{
							text: '关闭',
							handler: function(){
								detail_win.close();
							}
						}]
						
					});

					var node = Ext.getCmp('acc-win').layout.activeItem.selModel.selNode;
					var ss = node.id.split("|");  //业务部|王娅红|13862616066
					
					var pars = {name:ss[1]};
					new Ajax.Request("/desktop/get_detail_user", { 
						method: "POST",
						parameters: pars,
						onComplete:	 function(request) {
							var user = eval("("+request.responseText+")").users;
							Ext.getCmp('view_detail_form').items.items[5].setValue(user.login);
							Ext.getCmp('view_detail_form').items.items[6].setValue(user.bm);
							Ext.getCmp('view_detail_form').items.items[7].setValue(user.mobile);
							Ext.getCmp('view_detail_form').items.items[8].setValue(user.jtdz);
							Ext.getCmp('view_detail_form').items.items[9].setValue(user.email);
							detail_win.show();
						}
					});
					
				};


				var view_kh_detail = function() {
						var track_win = new Ext.Window({
								title: '客户信息',
								floating: true,
								shadow: true,
								draggable: true,
								closable: true,
								modal: true,
								width: 518,
								height: 395,
								layout: 'fit',
								plain: true,
								items: [{
									xtype:"form",
									id : 'lx_customer_add_form',
									width: 518,
							    height: 395,
							    padding: 10,
							    layout: 'absolute',
									items:[{
									    xtype: 'tabpanel',
											deferredRender: false,
									    activeTab: 0,
									    width: 500,
									    height: 320,
									    items: [
									        {
									            xtype: 'panel',
									            title: '客户信息',
									            layout: 'absolute',
									            width: 536,
									            height: 409,
									            items: [
																	{
																			xtype: 'textfield',
																			hidden : true,
																			name : 'id'
																	},
									                {
									                    xtype: 'label',
									                    text: '客户编号',
									                    x: 20,
									                    y: 10
									                },
									                {
									                    xtype: 'label',
									                    text: '联系人',
									                    x: 20,
									                    y: 40
									                },
									                {
									                    xtype: 'label',
									                    text: '职位',
									                    x: 20,
									                    y: 70
									                },
									                {
									                    xtype: 'label',
									                    text: '行业',
									                    x: 20,
									                    y: 100
									                },
									                {
									                    xtype: 'label',
									                    text: '移动电话',
									                    x: 20,
									                    y: 130
									                },
									                {
									                    xtype: 'label',
									                    text: 'iPhone',
									                    x: 20,
									                    y: 160
									                },
									                {
									                    xtype: 'label',
									                    text: '小灵通',
									                    x: 20,
									                    y: 190
									                },
									                {
									                    xtype: 'label',
									                    text: '家庭电话',
									                    x: 20,
									                    y: 220
									                },
									                {
									                    xtype: 'label',
									                    text: '其他电话',
									                    x: 20,
									                    y: 250
									                },
									                {
									                    xtype: 'label',
									                    text: '客户类别',
									                    x: 260,
									                    y: 10
									                },
									                {
									                    xtype: 'label',
									                    text: '电子邮件',
									                    x: 260,
									                    y: 40
									                },
									                {
									                    xtype: 'label',
									                    text: '生日',
									                    x: 260,
									                    y: 70
									                },
									                {
									                    xtype: 'label',
									                    text: '个人爱好',
									                    x: 260,
									                    y: 100
									                },
									                {
									                    xtype: 'label',
									                    text: '子女情况',
									                    x: 260,
									                    y: 130
									                },
									                {
									                    xtype: 'label',
									                    text: '家庭地址',
									                    x: 260,
									                    y: 160
									                },
									                {
									                    xtype: 'label',
									                    text: '备注',
									                    x: 260,
									                    y: 190
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 10,
									                    width: 150,
									                    name: 'khbh'
									                },
									                {
									                    xtype: 'combo',
									                    x: 320,
									                    y: 10,
									                    width: 150,
									                    name: 'khlb',
																			store: khlb_store,
																			emptyText:'',
																			mode: 'remote',
																			minChars : 2,
																			valueField:'value',
																			displayField:'value',
																			triggerAction:'all'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 40,
									                    width: 150,
									                    name: 'lxr'
									                },
									                {
									                    xtype: 'combo',
									                    x: 80,
									                    y: 70,
									                    width: 150,
									                    name: 'zw',
																			store: zwbh_store,
																			emptyText:'',
																			mode: 'remote',
																			minChars : 2,
																			valueField:'value',
																			displayField:'value',
																			triggerAction:'all'									
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 100,
									                    width: 150,
									                    name: 'hy'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 130,
									                    width: 150,
									                    name: 'mobile'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 160,
									                    width: 150,
									                    name: 'iphone'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 190,
									                    width: 150,
									                    name: 'linktone'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 220,
									                    width: 150,
									                    name: 'jtdh1'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 250,
									                    width: 150,
									                    name: 'jtdh2'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 320,
									                    y: 40,
									                    width: 150,
									                    name: 'email'
									                },
									                {
									                    xtype: 'datefield',
									                    x: 320,
									                    y: 70,
									                    width: 150,
																			format: 'Y-m-d',
									                    name: 'birthday'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 320,
									                    y: 100,
									                    width: 150,
									                    name: 'hobby'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 320,
									                    y: 130,
									                    width: 150,
									                    name: 'children'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 320,
									                    y: 160,
									                    width: 150,
									                    name: 'jtdz'
									                },
									                {
									                    xtype: 'textarea',
									                    x: 320,
									                    y: 190,
									                    width: 150,
									                    anchor: '',
									                    height: 80,
									                    name: 'bz'
									                }
									            ]
									        },
									        {
									            xtype: 'panel',
									            title: '公司信息',
									            layout: 'absolute',
									            items: [
									                {
									                    xtype: 'label',
									                    text: '公司名称',
									                    x: 20,
									                    y: 10
									                },
									                {
									                    xtype: 'label',
									                    text: '公司规模',
									                    x: 20,
									                    y: 40
									                },
									                {
									                    xtype: 'label',
									                    text: '公司全名',
									                    x: 20,
									                    y: 70
									                },
									                {
									                    xtype: 'label',
									                    text: '电话1',
									                    x: 20,
									                    y: 100
									                },
									                {
									                    xtype: 'label',
									                    text: '电话2',
									                    x: 20,
									                    y: 130
									                },
									                {
									                    xtype: 'label',
									                    text: '传真',
									                    x: 20,
									                    y: 160
									                },
									                {
									                    xtype: 'label',
									                    text: '网址',
									                    x: 20,
									                    y: 190
									                },
									                {
									                    xtype: 'label',
									                    text: '地址',
									                    x: 20,
									                    y: 220
									                },
									                {
									                    xtype: 'label',
									                    text: '邮编',
									                    x: 20,
									                    y: 250
									                },
									                {
									                    xtype: 'label',
									                    text: '开户银行',
									                    x: 260,
									                    y: 10
									                },
									                {
									                    xtype: 'label',
									                    text: '银行账号',
									                    x: 260,
									                    y: 40
									                },
									                {
									                    xtype: 'label',
									                    text: '税务登记号',
									                    x: 260,
									                    y: 70
									                },
									                {
									                    xtype: 'label',
									                    text: '所有制类型',
									                    x: 260,
									                    y: 100
									                },
									                {
									                    xtype: 'label',
									                    text: '客户级别',
									                    x: 260,
									                    y: 130
									                },
									                {
									                    xtype: 'label',
									                    text: '金土地内评',
									                    x: 260,
									                    y: 160
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 10,
									                    width: 150,
									                    name: 'gsmc'
									                },
									                {
									                    xtype: 'combo',
									                    x: 80,
									                    y: 40,
									                    width: 150,
									                    name: 'gsgm',
																			store: gsgm_store,
																			emptyText:'',
																			mode: 'remote',
																			minChars : 2,
																			valueField:'value',
																			displayField:'value',
																			triggerAction:'all'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 70,
									                    width: 150,
																			name: 'gsqm'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 100,
									                    width: 150,
									                    name: 'bgdh1'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 130,
									                    width: 150,
									                    name: 'bgdh2'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 160,
									                    width: 150,
									                    name: 'bgcz'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 190,
									                    width: 150,
									                    name: 'gsweb'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 220,
									                    width: 150,
									                    name: 'address'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 80,
									                    y: 250,
									                    width: 150,
									                    name: 'zip'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 330,
									                    y: 10,
									                    width: 150,
									                    name: 'khyh'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 330,
									                    y: 40,
									                    width: 150,
									                    name: 'yhzh'
									                },
									                {
									                    xtype: 'textfield',
									                    x: 330,
									                    y: 70,
									                    width: 150,
									                    name: 'swdjh'
									                },
									                {
									                    xtype: 'combo',
									                    x: 330,
									                    y: 100,
									                    width: 150,
									                    name: 'syzlx',
																			store: syzlx_store,
																			emptyText:'',
																			mode: 'remote',
																			minChars : 2,
																			valueField:'value',
																			displayField:'value',
																			triggerAction:'all'									
									                },
									                {
									                    xtype: 'combo',
									                    x: 330,
									                    y: 130,
									                    width: 150,
									                    name: 'khjb',
																			store: khjb_store,
																			emptyText:'',
																			mode: 'remote',
																			minChars : 2,
																			valueField:'value',
																			displayField:'value',
																			triggerAction:'all'									
									                },
									                {
									                    xtype: 'textfield',
									                    x: 330,
									                    y: 160,
									                    width: 150,
									                    name: 'jtdnp'
									                }
									            ]
									        }
									    ]
									}],
									buttons:[{
										text: '保存',
										handler: function(){
											var pars = Ext.getCmp("lx_customer_add_form").getForm().getValues();
											new Ajax.Request("/desktop/update_customer", { 
												method: "POST",
												parameters: pars,
												onComplete:	 function(request) {
													//track_win.close();
												}
											});
										}
									},{
										text: '取消',
										handler: function(){
											track_win.close();
										}
									}]
								}]
						});

						var node = Ext.getCmp('acc-win').layout.activeItem.selModel.selNode;
						var ss = node.id.split("|");  //业务部|王娅红|13862616066

						var pars = {khxx_id:ss[1]};
						new Ajax.Request("/desktop/set_customer", { 
							method: "POST",
							parameters: pars,
							onComplete:	 function(request) {
								var users = eval("("+request.responseText+")");
								if (users.size() > 0) {
									Ext.getCmp("lx_customer_add_form").getForm().setValues(users[0].users);
								}	
							}
						});
						track_win.show();						
					};
				
				var yg_menu = new Ext.menu.Menu({
					id : 'yg_menu',
					items:[{
						text:'发送短信',
						handler: send_message
					},{
						text:'查看详细',
						handler: view_detail
					}]
				});
				
				var yg_tree = Ext.getCmp('yg-tree');
				yg_tree.on("contextmenu", function(node,e) {
					//e.preventDefault();
					e.stopEvent();
					node.select();
					yg_menu.showAt(e.getXY());
				}, yg_tree);
				
				yg_tree.on("click", function(node,e) {
					//e.preventDefault();
					e.stopEvent();
					node.select();
					//yg_menu.showAt(e.getXY());
					if (Ext.getCmp('view_detail_form') != 'undefined') {
						var node = Ext.getCmp('acc-win').layout.activeItem.selModel.selNode;
						var ss = node.id.split("|");  //业务部|王娅红|13862616066

						var pars = {name:ss[1]};
						new Ajax.Request("/desktop/get_detail_user", { 
							method: "POST",
							parameters: pars,
							onComplete:	 function(request) {
								var user = eval("("+request.responseText+")").users;
								Ext.getCmp('view_detail_form').items.items[5].setValue(user.login);
								Ext.getCmp('view_detail_form').items.items[6].setValue(user.bm);
								Ext.getCmp('view_detail_form').items.items[7].setValue(user.mobile);
								Ext.getCmp('view_detail_form').items.items[8].setValue(user.jtdz);
								Ext.getCmp('view_detail_form').items.items[9].setValue(user.email);

							}
						});
					}
				}, yg_tree);
				
				var kh_menu = new Ext.menu.Menu({
					id : 'kh_menu',
					items:[{
						text:'发送短信',
						handler: send_message
					},{
						text:'查看详细',
						handler: view_kh_detail
					}]
				});
				
				var kh_tree = Ext.getCmp('kh-tree');
				kh_tree.on("contextmenu", function(node,e) {
					//e.preventDefault();
					e.stopEvent();
					node.select();
					kh_menu.showAt(e.getXY());
				}, kh_tree);

				kh_tree.on("click", function(node,e) {
					e.stopEvent();
					node.select();
					if (Ext.getCmp('lx_customer_add_form') != 'undefined') {
						var node = Ext.getCmp('acc-win').layout.activeItem.selModel.selNode;
						var ss = node.id.split("|");  

						var pars = {khxx_id:ss[1]};
						new Ajax.Request("/desktop/set_customer", { 
							method: "POST",
							parameters: pars,
							onComplete:	 function(request) {
								var users = eval("("+request.responseText+")");
								if (users.size() > 0) {
									Ext.getCmp("lx_customer_add_form").getForm().setValues(users[0].users);
								}
							}
						});
					}
				}, kh_tree);

				
				win.show();
		}
});

// for example purposes
var windowIndex = 0;

MyDesktop.BogusModule = Ext.extend(Ext.app.Module, {
		init : function(){
				this.launcher = {
						text: 'Window '+(++windowIndex),
						iconCls:'bogus',
						handler : this.createWindow,
						scope: this,
						windowId:windowIndex
				}
		},

		createWindow : function(src){
				var desktop = this.app.getDesktop();
				var win = desktop.getWindow('bogus'+src.windowId);
				if(!win){
						win = desktop.createWindow({
								id: 'bogus'+src.windowId,
								title:src.text,
								width:640,
								height:480,
								html : '<p>Something useful would be in here.</p>',
								iconCls: 'bogus',
								shim:false,
								animCollapse:false,
								constrainHeader:true
						});
				}
				win.show();
		}
});


MyDesktop.BogusMenuModule = Ext.extend(MyDesktop.BogusModule, {
		init : function(){
				this.launcher = {
						text: 'Bogus Submenu',
						iconCls: 'bogus',
						handler: function() {
							return false;
						},
						menu: {
								items:[{
										text: 'Bogus Window '+(++windowIndex),
										iconCls:'bogus',
										handler : this.createWindow,
										scope: this,
										windowId: windowIndex
										},{
										text: 'Bogus Window '+(++windowIndex),
										iconCls:'bogus',
										handler : this.createWindow,
										scope: this,
										windowId: windowIndex
										},{
										text: 'Bogus Window '+(++windowIndex),
										iconCls:'bogus',
										handler : this.createWindow,
										scope: this,
										windowId: windowIndex
										},{
										text: 'Bogus Window '+(++windowIndex),
										iconCls:'bogus',
										handler : this.createWindow,
										scope: this,
										windowId: windowIndex
										},{
										text: 'Bogus Window '+(++windowIndex),
										iconCls:'bogus',
										handler : this.createWindow,
										scope: this,
										windowId: windowIndex
								}]
						}
				}
		}
});

var prev_store = new Ext.data.Store({

    proxy: new Ext.data.HttpProxy({
        url: '/desktop/get_prev'
    }),

		reader: new Ext.data.JsonReader({
			totalProperty: 'results', 
			root: 'rows',							
			fields: [
				{name: 'id'},
				{name: 'tabname',	type: 'string'},
				{name: 'addprev',	type: 'string'},
				{name: 'delprev',	type: 'string'},
				{name: 'modprev',	type: 'string'},
				{name: 'viewprev',type: 'string'},
				{name: 'advprev',	type: 'string'}
			]		 
		}),
		baseParams : {yg_id:1},
		pruneModifiedRecords:true,
		sortInfo:{field: 'id', direction: "ASC"}
}); 

prev_store.load();

var render_b = function(v) {
	return v == 't';
}

MyDesktop.PreferenceWindow = Ext.extend(Ext.app.Module, {
	id:'grid-prev',
	init : function(){
			this.launcher = {
					text: '权限设置',
					iconCls:'icon-pref',
					handler : this.createWindow,
					scope: this
			}
	},
	

	createWindow : function(){

			if (currentUser.prev_code.substring(0,5) != 'ttttt') {
					//Ext.Msg.alert('Message','权限不够. 请与管理员联系后再试！');
					msg('Message','权限不够. 请与管理员联系后再试！');
					return;
			}
			
			var desktop = this.app.getDesktop();
			var win = desktop.getWindow('grid-pref');
			if(!win){
					win = desktop.createWindow({
							id: 'grid-prev',
							title:'权限设置',
							x:100,
							width:550,
							height:380,
							iconCls: 'icon-pref',
							shim:false,
							animCollapse:false,
							border:false,
							constrainHeader:true,
							layout:"border",
							items:[{
							    region:"center",
							    items:[{
										layout: 'absolute',
										xtype: 'form',
										id: 'prev_set_form',
										x: 0,
										y: 0,
										width : 350,
										height : 200,
										border : false, 
										items: [
										  {x:10, y:15, text: '系统管理', xtype: 'label'},
										  {x:10, y:45, text: '客源管理', xtype: 'label'},
										  {x:10, y:75, text: '房源管理', xtype: 'label'},
										  {x:10, y:105, text: '联系人', xtype: 'label'},
										  {x:10, y:135, text: '排行榜', xtype: 'label'},
											{
										    xtype: 'checkboxgroup',
										 		name: 'type-1',
												columns: 5,
												x: 80,
												y: 10,  
										    items: [ 
										        {boxLabel: '增加', name: '11', checked: true}, 
										        {boxLabel: '删除', name: '12'},
										        {boxLabel: '修改', name: '13'},
										        {boxLabel: '查看', name: '14'},
										        {boxLabel: '高级', name: '15'} 
										    ] 
										 },{
										    xtype: 'checkboxgroup',
										 		name: 'type-2',
										    x: 80,
										    y: 40,
												columns: 5, 
										    items: [ 
										        {boxLabel: '增加', name: '21', checked: true}, 
										        {boxLabel: '删除', name: '22'},
										        {boxLabel: '修改', name: '23'},
										        {boxLabel: '查看', name: '24'},
										        {boxLabel: '高级', name: '25'} 
										    ] 
										 },{
										    xtype: 'checkboxgroup',
										 		name: 'type-3',
										    x: 80,
										    y: 70,
												columns: 5, 
										    items: [ 
										        {boxLabel: '增加', name: '31', checked: true}, 
										        {boxLabel: '删除', name: '32'},
										        {boxLabel: '修改', name: '33'},
										        {boxLabel: '查看', name: '34'},
										        {boxLabel: '高级', name: '35'} 
										    ]
										 },{
										    xtype: 'checkboxgroup',
										 		name: 'type-4',
										    x: 80,
										    y: 100,
												columns: 5, 
										    items: [ 
										        {boxLabel: '增加', name: '41', checked: true}, 
										        {boxLabel: '删除', name: '42'},
										        {boxLabel: '修改', name: '43'},
										        {boxLabel: '查看', name: '44'},
										        {boxLabel: '高级', name: '45'} 
										    ]
										 },{
										    xtype: 'checkboxgroup',
										 		name: 'type-5',
										    x: 80,
										    y: 130,
												columns: 5, 
										    items: [ 
										        {boxLabel: '增加', name: '51', checked: true}, 
										        {boxLabel: '删除', name: '52'},
										        {boxLabel: '修改', name: '53'},
										        {boxLabel: '查看', name: '54'},
										        {boxLabel: '高级', name: '55'} 
										    ]
										 },{
										    xtype: 'checkboxgroup',
										 		name: 'type-6',
										    x: 80,
										    y: 160,
												columns: 5, 
										    items: [ 
										        {boxLabel: '增加', name: '61', checked: true}, 
										        {boxLabel: '删除', name: '62'},
										        {boxLabel: '修改', name: '63'},
										        {boxLabel: '查看', name: '64'},
										        {boxLabel: '高级', name: '65'} 
										    ]
										},{x:10, y:165, text: '日程安排', xtype: 'label'},
											{
												xtype: 'textfield',
												name: 'yg_id',
												hidden: true
										}]
										/*
										items: [{
														xtype: 'editorgrid',
														id : 'prev_editor_grid',
														height: 350,
														store : prev_store,
														sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
														columns: [
															{	xtype: 'gridcolumn', dataIndex: 'id', header: 'id', sortable: true, width: 50, hidden:true, editor: { xtype: 'textfield'}},
															{	xtype: 'gridcolumn', dataIndex: 'tabname', header: '管理内容', sortable: true, width: 100, editor: { xtype: 'textfield'}},
															{ xtype: 'booleancolumn', dataIndex: 'addprev', header: '增加', sortable: true, width: 50, editor: { xtype: 'checkbox',boxLabel: '有'}, trueText:'有',falseText:'无'},
															{	xtype: 'booleancolumn', dataIndex: 'delprev', header: '删除', sortable: true, width: 50, editor: { xtype: 'checkbox',boxLabel: '有'}, trueText:'有',falseText:'无'},
															{	xtype: 'booleancolumn', dataIndex: 'modprev', header: '修改', sortable: true, width: 50, editor: { xtype: 'checkbox',boxLabel: '有'}, trueText:'有',falseText:'无'},
															{	xtype: 'booleancolumn', dataIndex: 'viewprev', header: '查看', sortable: true, width: 50, editor: { xtype: 'checkbox',boxLabel: '有'}, trueText:'有',falseText:'无'},
															{	xtype: 'booleancolumn', dataIndex: 'advprev', header: '高级', sortable: true, width: 50, editor: { xtype: 'checkbox',boxLabel: '有'}, trueText:'有',falseText:'无'}
														],
														bbar:[{
															xtype: 'toolbar',
															items: [{
																xtype: 'button',
																iconCls:'publish',
																text: '保存',
																handler: function() {
																"系统管理"
																"客源管理"
																"房源管理"
																"联系人"
																"排行榜"
																
																}
															}]				
														}]
												}] */
							      }],
										tbar: {
												xtype: 'toolbar',
												items: [
														{
																xtype: 'button',
																text: '保存',
																tooltip:'保存权限设置',
																iconCls:'search',
																handler : function() {
																	var group = Ext.getCmp('prev_set_form');
																	var txt="";
																	for (var k=5; k<11; k++) {
																		for (var l=0; l<5; l++) {
																			var str =  (group.items.items[k].items.items[l].el.dom.checked) ? 't' : 'f';
																			txt = txt + str; 
																		}
																	}
																	var pars = {code:txt, yg_id:Ext.getCmp('prev_set_form').form.findField('yg_id').getValue()};
																	new Ajax.Request("/desktop/save_prev_code", { 
																		method: "POST",
																		parameters: pars,
																		onComplete:	 function(request) {
																			
																		}
																	});																	
																	
																}
														} /*,
														{
																xtype: 'button',
																text: '上一个',
																tooltip:'上一个',
																iconCls:'add2',
																handler:function() {
																}
														},
														{
																xtype: 'button',
																text: '下一个',
																tooltip:'下一个',
																iconCls:'delete',
																handler:function() {
																}
														}*/
												]
										}
										
							  },{
							    region:"west",
							    title:"用户列表",
							    width:150,
							    split:true,
									items :[{
								    xtype: 'treepanel',
										rootVisible:false,
										lines:false,
										id : 'yg-prev-tree',
										autoScroll:true,
										height:380,
								    root: {
								        nodeType: 'async',
												text: '联系人',
												draggable:false,
												id:'root'
								    },
								    loader: new Ext.tree.TreeLoader({
											dataUrl: '/desktop/get_prevtree',
											baseAttrs: { uiProvider: Ext.ux.TreeCheckNodeUI } 
										})										
									}]
							  }]
					});
			}
			
			var yg_tree = Ext.getCmp('yg-prev-tree');

			yg_tree.on("click", function(node,e) {
				e.preventDefault();
				e.stopEvent();
				node.select();
				Ext.getCmp('grid-prev').setTitle ('权限设置 -- '+node.text);

				Ext.getCmp('prev_set_form').form.findField('yg_id').setValue(node.id);
				//prev_store.baseParams={yg_id:node.id};
				//prev_store.reload();
				
				pars = {yg_id:node.id};
				new Ajax.Request("/desktop/get_prev_code", { 
					method: "POST",
					parameters: pars,
					onComplete:	 function(request) {
						var code = request.responseText;
						var group = Ext.getCmp('prev_set_form');
						for (var k=5; k<11; k++) {
							for (var l=0; l<5; l++) {
								var kk = (k-5)*5+l;
								var checked=(code.substring(kk,kk+1) == 't')
								group.items.items[k].items.items[l].el.dom.checked = checked;
							}
						}
					}
				});
				
				
			}, yg_tree);
			
			win.show();
	}
	
});

